package com.shangcheng.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.shangcheng.goods.dao.BrandMapper;
import com.shangcheng.goods.dao.SkuMapper;
import com.shangcheng.goods.dao.SpuMapper;
import com.shangcheng.goods.pojo.*;
import com.shangcheng.goods.service.SpuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.IdWorker;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    private BrandMapper brandMapper;


    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void saveGoods(Goods goods) {
        //增加spu
        Spu spu = goods.getSpu();
        if (spu.getId() == null) {
            spu.setId(idWorker.nextId());
            spuMapper.insertSelective(spu);
        } else {
            //修改数据
            spuMapper.updateByPrimaryKeySelective(spu);
            Sku sku = new Sku();
            sku.setSpuId(spu.getId());
            skuMapper.delete(sku);
        }

        //增加sku
        Date date = new Date();
        Brand brand = brandMapper.selectByPrimaryKey(spu.getBrandId());
        List<Sku> skuList = goods.getSkuList();
        for (Sku sku : skuList) {
            if (StringUtil.isEmpty(sku.getSpec())) {
                sku.setSpec("{}");
            }
            String name = spu.getName();
            Map<String, String> specMap = JSON.parseObject(sku.getSpec(), Map.class);
            for (Map.Entry<String, String> entry : specMap.entrySet()) {
                name += " " + entry.getValue();
            }
            sku.setName(name);
            sku.setId(idWorker.nextId());
            sku.setSn(spu.getSn());
            sku.setBrandName(brand.getName());
            sku.setSpuId(spu.getId());
            sku.setPrice(sku.getPrice());
            sku.setCreateTime(date);
            sku.setUpdateTime(date);
            skuMapper.insertSelective(sku);
        }
    }

    @Override
    public PageInfo<Goods> findPageGoods(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        List<Spu> spuList = spuMapper.selectAll();
        Sku sku = new Sku();
        List<Goods> goodsList = new ArrayList<Goods>();
        for (Spu spu : spuList) {
            if (spu.getIsDelete().equals("1")) {
                continue;
            }
            Goods goods = new Goods();
            sku.setSpuId(spu.getId());
            List<Sku> skuList = skuMapper.select(sku);
            goods.setSpu(spu);
            goods.setSkuList(skuList);
            goodsList.add(goods);
        }
        //执行搜索
        return new PageInfo<Goods>(goodsList);
    }

    @Override
    public Goods findGoodsById(Long id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);
        Sku sku = new Sku();
        sku.setSpuId(spu.getId());
        List<Sku> skuList = skuMapper.select(sku);
        Goods goods = new Goods();
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        return goods;
    }


    @Override
    public void audit(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        if (spu.getIsDelete().equalsIgnoreCase("1")) {
            throw new RuntimeException("该商品已经删除");
        }
        spu.setStatus("1");//通过审核
        spu.setIsMarketable("1");//上架
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public void pull(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        if (spu.getIsDelete().equalsIgnoreCase("1")) {
            throw new RuntimeException("该商品已经删除");
        }
        spu.setIsMarketable("0");//下架
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public void put(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        if (spu.getIsDelete().equals("1")) {
            throw new RuntimeException("该商品已经删除");
        }
        if (!spu.getStatus().equals("1")) {
            throw new RuntimeException("未通过审核的商品不能");
        }
        spu.setIsMarketable("1");//上架
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    @Transactional
    public void logicDelete(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        if (spu.getIsMarketable().equals("1")) {
            throw new RuntimeException("上架商品无法删除");
        }
        spu.setIsDelete("1");
        spu.setStatus("0");
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public void restore(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        if (!spu.getIsDelete().equals("1")) {
            throw new RuntimeException("此商品未删除");
        }
        //未删除
        spu.setIsDelete("0");
        //未审核
        spu.setStatus("0");
        spuMapper.updateByPrimaryKeySelective(spu);

    }

    @Override
    public PageInfo<Spu> findPageById(int id, int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //执行搜索
        return new PageInfo<Spu>(spuMapper.selectById(id));
    }

    @Override
    public PageInfo<Spu> findPageIsMarketable(String isMarketable, int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //执行搜索
        return new PageInfo<Spu>(spuMapper.findPageIsMarketable(isMarketable));
    }

    @Override
    public PageInfo<Spu> findPageByStatus(String status, int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //执行搜索
        return new PageInfo<Spu>(spuMapper.selectByStatus(status));
    }

    @Override
    public PageInfo<Spu> findPage(Spu spu, int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        Example example = createExample(spu);
        //执行搜索
        return new PageInfo<Spu>(spuMapper.selectAll());
    }

    @Override
    public PageInfo<Spu> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        ;
        //执行搜索
        return new PageInfo<Spu>(spuMapper.selectAll());
    }

    @Override
    public int addSC(String username, String id) {
        return spuMapper.addSC(username, id);
    }

    @Override
    public int deleteSC(String id) {
        return spuMapper.deleteSC(id);
    }

    private Example createExample(Spu spu) {
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
        if (spu != null) {
            // 模板名称
            if (!StringUtils.isEmpty(spu.getName())) {
                criteria.andLike("name", "%" + spu.getName() + "%");
            }
            if (!StringUtils.isEmpty(spu.getSn())) {
                criteria.andEqualTo("sn", "%" + spu.getSn() + "%");
            }
            if (!StringUtils.isEmpty(String.valueOf(spu.getBrandId()))) {
                criteria.andEqualTo("brand_id", spu.getBrandId());
            }
        }
        return example;
    }

}
